package com.sm.filemanagelibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.scorpio.baselibrary.PoorApplication;
import com.scorpio.baselibrary.ui.BasicTitleActivity;
import com.scorpio.baselibrary.utils.FileSizeUtil;
import com.sm.filemanagelibrary.adapter.FolderLiseAdapter;
import com.sm.filemanagelibrary.fragment.FileOverallFragment;
import com.sm.filemanagelibrary.model.FileEntity;
import com.sm.filemanagelibrary.view.ImageFilePagerDialog;
import com.sm.filemanagelibrary.view.TabSelectedLayout;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class FileSelectedActivity extends BasicTitleActivity implements TabSelectedLayout.OnTabItemClickListener {

    private TabSelectedLayout tabSelectedLayout;
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentManager fm;
    private Fragment letFragment;
    private Set<FileEntity> selectedFile = new HashSet<>();
    private SelectedFileBroadcastReceiver selectedFileBroadcastReceiver;
    private ImageFilePagerDialog filePagerDialog;


    @Override
    protected int onCreateLayout() {
        return R.layout.activity_file_selected;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        setBasicTitle(R.string.file_string_00001);
        fragments.clear();
        fragments.add(FileOverallFragment.getFileOverallFragment(0));
        fragments.add(FileOverallFragment.getFileOverallFragment(1));
        fragments.add(FileOverallFragment.getFileOverallFragment(2));
        fragments.add(FileOverallFragment.getFileOverallFragment(3));
        fragments.add(FileOverallFragment.getFileOverallFragment(4));
        fm = getSupportFragmentManager();
        tabSelectedLayout = v.getView(R.id.tabSelectedLayout);
        tabSelectedLayout.setOnTabItemClickListener(this);
        tabSelectedLayout.selected(0);
        v.addOnClickListener(this, R.id.filePreviewBtn);
        filePagerDialog = new ImageFilePagerDialog(this);
        registerReceiver(selectedFileBroadcastReceiver = new SelectedFileBroadcastReceiver()
                , new IntentFilter(FCConstant.SelectedFileBroadcastReceiverTAG));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (selectedFileBroadcastReceiver != null)
            unregisterReceiver(selectedFileBroadcastReceiver);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.filePreviewBtn) {
            List<String> images = new ArrayList<>();
            for (FileEntity entity : selectedFile) {
                if (entity.getItemType() == FolderLiseAdapter.TYPE_IMAGE) images.add(entity.path);
            }
            if (images.size() == 0) return;
            filePagerDialog.show(images, 0);
        }
    }

    @Override
    public void OnTabItemClick(int index, String meun) {
        Fragment fragment = fm.findFragmentByTag(meun);
        if (fragment == null) {
            fragment = fragments.get(index);
            fm.beginTransaction().add(R.id.fram_layout, fragment, meun).commit();
        }
        if (letFragment != null) {
            fm.beginTransaction().hide(letFragment).commit();
        }
        letFragment = fragment;
        fm.beginTransaction().show(fragment).commit();
    }

    public class SelectedFileBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra("state", -1);
            FileEntity entity = intent.getParcelableExtra("data");
            if (state == -1 || entity == null) return;
            synchronized (selectedFile) {
                if (state == FCConstant.SELECTED_FILE_STATE_ADD) {
                    selectedFile.add(entity);
                } else if (state == FCConstant.SELECTED_FILE_STATE_DEL) {
                    for (FileEntity fileEntity : selectedFile) {
                        if (fileEntity.path.equals(entity.path)) {
                            selectedFile.remove(fileEntity);
                            break;
                        }
                    }
                }
                updateSelectedFileState();
            }
        }
    }

    public static void onSelectedEvent(FileEntity entity, @FCConstant.SelectedFileState int state) {
        Intent intent = new Intent();
        intent.putExtra("state", state);
        intent.putExtra("data", entity);
        intent.setAction(FCConstant.SelectedFileBroadcastReceiverTAG);
        PoorApplication.getInstance().sendBroadcast(intent);
    }

    public void updateSelectedFileState() {
        long fileSize = 0;
        int fileCount = selectedFile.size();
        boolean isExistImage = false;
        v.setGone(R.id.filePreviewBtn, false);
        if (fileCount == 0) {
            v.setInvisible(R.id.fileInfoText, false)
                    .setEnabled(R.id.fileAffirmBtn, false)
                    .setText(R.id.fileAffirmBtn, String.format(getString(R.string.file_string_00013), "0"));
            return;
        }
        for (FileEntity entity : selectedFile) {
            fileSize += entity.size;
            if (entity.getItemType() == FolderLiseAdapter.TYPE_IMAGE) isExistImage = true;
        }
        v.setText(R.id.fileInfoText, String.format(getString(R.string.file_string_00012), FileSizeUtil.FormetFileSize(fileSize)))
                .setText(R.id.fileAffirmBtn, String.format(getString(R.string.file_string_00013), "" + fileCount))
                .setInvisible(R.id.fileInfoText, true)
                .setGone(R.id.fileAffirmBtn, true)
                .setEnabled(R.id.fileAffirmBtn, true)
                .setGone(R.id.filePreviewBtn, isExistImage);
    }
}
